package com.gopay.common.util;

import javax.annotation.Resource;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;

/**
 * Spring Context相关工具
 * 
 * @ContextUtil.java
 * @author zhanglei
 * @2013-1-23 下午3:03:50 www.gopay.com.cn Inc.All rights reserved.
 */
@Resource
public class ContextUtil implements BeanFactoryAware {
    private static BeanFactory beanFactory;

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        ContextUtil.beanFactory = beanFactory;
    }

    public static BeanFactory getBeanFactory() {
        return beanFactory;
    }

    public static Object getBean(String name) {
        if (null == beanFactory) {
            throw new RuntimeException("BeanFactory has not been initialized!!");
        }
        return beanFactory.getBean(name);
    }

}
